#include <iostream>
#include <assert.h>
#include <algorithm>

using namespace std;

namespace my_vector
{
    template<class T>
    class vector 
    {
    public:
        typedef T* iterator;
        typedef const T* const_iterator;
        typedef T& reference;
        typedef const T& const_reference;
        typedef vector<T> Self;
        typedef vector<T>& Self_reference;
        typedef const vector<T> const_Self;
        typedef const vector<T>& const_Self_reference;
        
        iterator begin() { return _start; }
        const_iterator begin() const { return _start; }
        iterator end() { return _finish; }
        const_iterator end() const { return _finish; }

        void swap(Self_reference v)
        {
            ::swap(_start, v._start);
            ::swap(_finish, v._finish);
            ::swap(_end_of_storage, v._end_of_storage);
        }

        vector() :_start(nullptr), _finish(nullptr), _end_of_storage(nullptr) {}

        template<class InputIterator>
        vector(InputIterator first, InputIterator last)
            :_start(nullptr)
             ,_finish(nullptr)
             ,_end_of_storage(nullptr)
        {
            while (first != last)
            {
                push_back(*first);
                first ++ ;
            }
        }

        vector(const_Self_reference v)
            :_start(nullptr)
             ,_finish(nullptr)
             ,_end_of_storage(nullptr)
        {
            Self tmp(v.begin(), v.end());
            swap(tmp);
        }

        vector(size_t n, const_reference x = T())
            :_start(nullptr)
             ,_finish(nullptr)
             ,_end_of_storage(nullptr)
        {
            reserve(n);
            for (size_t i = 0; i < n; i ++ )
                push_back(x);
        }
        vector(int n, const_reference x = T())
            :_start(nullptr)
             ,_finish(nullptr)
             ,_end_of_storage(nullptr)
        {
            reserve(n);
            for (int i = 0; i < n; i ++ )
                push_back(x);
        }

        ~vector()
        {
            delete[] _start;
            _start = _finish = _end_of_storage = nullptr;
        }

        Self_reference operator=(Self v)
        {
            swap(v);
            return *this;
        }

        void push_back(const_reference x)
        {
            if (_finish == _end_of_storage)
                reserve(capacity() == 0 ? 4 : capacity() * 2);

            *_finish = x;
            _finish ++ ;
        }

        void pop_back() { assert(!empty()); _finish -- ; }

        iterator insert(iterator pos, const_reference x)
        {
            assert(pos >= _start && pos <= _finish);

            size_t oldpos = pos - _start;
            if (_finish == _end_of_storage)
            {
                reserve(capacity() == 0 ? 4 : capacity() * 2);
            }

            pos = _start + oldpos;
            iterator end = _finish;
            while (end > pos)
            {
                *end = *(end - 1);
                end -- ;
            }

            *pos = x;
            _finish ++ ;

            return pos;
        }

        iterator erase(iterator pos)
        {
            assert(!empty() && pos >= _start && pos < _finish);

            iterator begin = pos;
            while (begin < _finish - 1)
            {
                *begin = *(begin + 1);
                begin ++ ;
            }
            _finish -- ;

            return pos;
        }

        void reserve(size_t n)
        {
            if (n > size())
            {
                //Self tmp;
                //tmp._start = new T[n];
                //size_t oldsize = size();

                //for (size_t i = 0; i < oldsize; i ++ )
                //    tmp._start[i] = _start[i];

                //tmp._finish = tmp._start + oldsize;
                //tmp._end_of_storage = tmp._start + n;

                //swap(tmp);
                
                iterator tmp = new T[n];
                size_t oldsize = size();

                if (_start)
                {
                    for (size_t i = 0; i < oldsize; i ++ )
                        tmp[i] = _start[i];

                    delete[] _start;
                }

                _start = tmp;
                _finish = _start + oldsize;
                _end_of_storage = _start + n;
            }   
        }

        void resize(size_t n, const_reference x = T())
        {
            if (n > capacity())
                reserve(n);

            if (size() > n)
            {
                _finish = _start + n;
            }
            else 
            {
                for (size_t i = size(); i < n; i ++ )
                    _start[i] = x;

                _finish = _start + n;
            }
        }

        reference operator[](size_t pos)
        {
            assert(pos < size());
            return _start[pos];
        }
        const_reference operator[](size_t pos) const 
        {
            assert(pos < size());
            return _start[pos];
        }

        bool empty() const { return _start == _finish; }
        size_t size() const { return _finish - _start; }
        size_t capacity() const { return _end_of_storage - _start; }
        
        reference back() { assert(!empty()); return *(_finish - 1); }
        reference front() { assert(!empty()); return *_start; }

        void print()
        {
            for (size_t i = 0; i < size(); i ++ )
                cout << _start[i];
            cout << endl;
        }

    private:
        iterator _start;
        iterator _finish;
        iterator _end_of_storage;
    };

        void test1()
    {
        vector<int> v;
        v.push_back(1);
        v.push_back(2);
        v.push_back(3);
        v.push_back(4);
        v.push_back(5);
        v.push_back(6);
        v.push_back(7);

        for (size_t i = 0; i < v.size(); i ++ )
        {
            cout << v[i] << ' ';
        }
        cout << endl;
        
        vector<int>::iterator it = v.begin();
        while (it != v.end())
        {
            cout << *it << ' ';
            ++ it;
        }
        cout << endl;
    }

    void test2()
    {
        vector<int> v1(10, 1);
        for (size_t i = 0; i < v1.size(); i ++ )
        {
            cout << v1[i] << ' ';
        }
        cout << endl;

        vector<int> v2;
        v2.push_back(100);
        v2.push_back(199);
        v2.push_back(200);
        v2.push_back(198);
        v2.push_back(999);
        v2.push_back(555);

        for (size_t i = 0; i < v2.size(); i ++ )
        {
            cout << v2[i] << ' ';
        }
        cout << endl;

        vector<int> v3 = v2;
        for (size_t i = 0; i < v3.size(); i ++ )
        {
            cout << v3[i] << ' ';
        }
        cout << endl;

        v2 = v1;
        for (size_t i = 0; i < v1.size(); i ++ )
        {
            cout << v1[i] << ' ';
        }
        cout << endl;
    }

    void test3()
    {
        vector<int> v;
        v.reserve(20);
        cout << v.capacity() << endl;
        v.push_back(1);
        v.push_back(2);
        v.push_back(3);
        v.push_back(4);
        v.push_back(5);
        v.push_back(6);
        v.push_back(7);
        v.push_back(8);
        cout << v.capacity() << endl;
        for (size_t i = 0; i < v.size(); i ++ )
        {
            cout << v[i] << endl;
        }
        cout << endl;

        v.insert(find(v.begin(), v.end(), 6), 99999);
        v.erase(find(v.begin(), v.end(), 8));

        for (size_t i = 0; i < v.size(); i ++ )
        {
            cout << v[i] << ' ';
        }
        cout << endl;
    }
}
